package main.java;

/**
 * @program: pratice
 * @author: 关键我是大怪
 * @create: 2021-07-20 19:39
 */
public class Test06_替换空格 {


    /**
     * 思路
     * 将String类型转换为StringBuffer对象
     * 遍历StringBuffer对象 存在” “替换为%20
     * 利用StringBuffer类对象的toString（）方法转为String类型
     * 返回String字符串
     */


    public static String test(String s) {

        StringBuffer s1 = new StringBuffer(s);
        for (int i = 0; i < s1.length(); i++) {
            if (s1.charAt(i) == ' ') {
                s1.replace(i, i + 1, "%20");
            }
        }
        s = s1.toString();
        return s;
    }

    public static void main(String[] args) {
        String s = "We are happy.";
        System.out.println(test(s));
    }

}
